package hu.veilingsite.gui;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.shared.Auction;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class ShowActiveAuctions {
	
	public void getNew(){
		
		final ServiceAsync myService = (ServiceAsync) GWT.create(Service.class);
		AsyncCallback<ArrayList<Auction>> callback = new AsyncCallback<ArrayList<Auction>>(){

			@Override
			public void onFailure(Throwable caught) {
				RootPanel.get("veilingen").clear();
				RootPanel.get("veilingen").add(new HTML("Fout bij het ophalen van de laatste veilingen."));
			}

			@Override
			public void onSuccess(ArrayList<Auction> result) {
				if(result.size() > 0){
					RootPanel.get("veilingen").clear();
					RootPanel.get("veilingen").add(new HTML("<h1>Meest recente veilingen</h1>"));
					for(int I = 0; I <= result.size() - 1; I++){
						Auction curAu = result.get(I);
						Date until = new Date(curAu.getEndDate().getTime());
						Date now = new Date();
						String tijdover = "";
						long timeleft = until.getTime() - now.getTime();
						if(timeleft > 0){
							// Easy as that ;D
							long days = (long) Math.ceil(timeleft / (1000 * 60 * 60 * 24));
							timeleft = timeleft - (days * (1000 * 60 * 60 * 24));
							if(days > 0) tijdover += days + " dagen ";
							long hours = (long) Math.ceil(timeleft / (1000 * 60 * 60));
							timeleft = timeleft - (hours * (1000 * 60 * 60));
							if(hours > 0) tijdover += hours + " uur ";
							long min = (long) Math.ceil(timeleft / (1000 * 60));
							timeleft = timeleft - (min * (1000 * 60));
							if(min > 0) tijdover += min + " min ";
							long sec = (long) Math.ceil(timeleft / (1000));
							timeleft = timeleft - (sec * (1000));
							tijdover += sec + " sec.";
						}
						else{
							tijdover = "Veiling gesloten.";
						}
						RootPanel.get("veilingen").add(new HTML("<h3>" + curAu.getTitle() + "</h3><b>Gestart met:</b> " + curAu.getStartprice() + " credits<br /><b>Loopt tot:</b> " + curAu.getEndDate().toString() + "<br /><b>Tijd over:</b> " + tijdover + "<br />"));
						Button goTo = new Button("Bekijk");
						goTo.addStyleName("epicButton");
						final int curid = curAu.getId();
						goTo.addClickHandler(new ClickHandler(){
							@Override
							public void onClick(ClickEvent event) {
								RootPanel.get("main").clear();
								RootPanel.get("main").add(new ViewAuction(curid).createPage());
							}
						});
						RootPanel.get("veilingen").add(goTo);
						RootPanel.get("veilingen").add(new HTML("<br /><br />"));
					}
				}
				else{
					RootPanel.get("veilingen").clear();
					RootPanel.get("veilingen").add(new HTML("Er staan nog geen veilingen in het systeem, maak een veiling aan en val gegarandeert op in dit blok!"));
				}
			}
		};
		myService.getLatestAcutions(callback);
	}

	public ShowActiveAuctions() {
	}
}